package main

import (
	"time"
	"fmt"
	"sync"
)

var (
	resultMap = make(map[int]uint64, 10)
	lock      = sync.Mutex{}
)
//该案例会出现 fatal error: concurrent map writes
func calc(n int) {
	result := uint64(1)
	for i := 1; i <= n; i++ {
		result *= uint64(i)
	}
	resultMap[n] = result

}

//加了全局锁，防止出现并发写入
func lockcalc(n int) {
	result := uint64(1)
	for i := 1; i < n; i++ {
		result *= uint64(i)
	}
	lock.Lock()
	resultMap[n] = result
	lock.Unlock()

}

//计算1-200的阶乘

func main() {
	for i := 1; i <= 200; i++ {
		go lockcalc(i)
	}
	time.Sleep(5 * time.Second)

	for i, v := range resultMap {
		fmt.Printf("索引:%v 值:%v \n", i, v)
	}
}
